<template>
  <div class="table-wrap" v-loading="loading">
    <div class="search_header">
      <div class="search_header_left">
        <el-button
          icon="el-icon-plus"
          @click="addProject"
          v-has="'industrialCarrier:add'"
          >新增</el-button
        >
        <el-button
          icon="el-icon-download"
          @click="downloadFile"
          v-has="'industrialCarrier:export'"
          >下载数据</el-button
        >
        <el-button
          icon="el-icon-refresh"
          @click="trackProject"
          v-has="'industrialCarrier:track'"
          >进度填报</el-button
        >
      </div>

      <div class="search_header_right">
        <el-input
          placeholder="请输入园区/楼宇名称"
          v-model="parameter.name"
          @keydown.enter.native="search"
          clearable
          @clear="search"
        >
          <span
            @click="search"
            slot="suffix"
            style="display: inline-block"
          >
            <Icon
              title="搜索"
              name="sousuo"
              color="#A7ADB5"
              font-size="20"
            />
          </span>
        </el-input>
      </div>
    </div>
    <el-table
      :data="projectList"
      :header-cell-style="{
        background: '#F2F6FB',
        color: ' #6B767D'
      }"
      :row-class-name="tableRowClassName"
    >
      <el-table-column
        label="序号"
        align="center"
        show-overflow-tooltip
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="所属项目(园区)"
        prop="parkName"
        show-overflow-tooltip
      />
      <el-table-column
        label="楼宇"
        prop="name"
        show-overflow-tooltip
      />
      <el-table-column
        label="入驻企业"
        prop="enterpriseNum"
        show-overflow-tooltip
      >
        <template #default="scope">
          <div
            class="link-enterprise"
            @click="handleEnterprise(scope.row)"
          >
            {{ scope.row.enterpriseNum || 0 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="建筑面积(平方米)"
        prop="buildArea"
        show-overflow-tooltip
      />
      <el-table-column
        label="可租面积(平方米)"
        prop="rentArea"
        show-overflow-tooltip
      />
      <el-table-column
        label="本年度已去化面积"
        prop="addMonthArea"
        show-overflow-tooltip
      />
      <el-table-column
        label="年度累计已去化面积"
        prop="addMonthTotalArea"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        show-overflow-tooltip
        width="240"
      >
        <template slot-scope="scope">
          <el-button
            @click="viewProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >查看</el-button
          >
          <el-button
            @click="editProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            v-has="'industrialCarrier:edit'"
            >编辑</el-button
          >
          <el-button
            @click="deleteProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            v-has="'industrialCarrier:delete'"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination" v-if="total">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="parameter.pageSize"
        @current-change="pageChange"
        :total="total"
        :current-page="parameter.pageIndex"
      >
      </el-pagination>
    </div>

    <AddEnterpriseDialog
      :visible.sync="showDialog"
      :title="title"
      :currentData="currentData"
      :isDetail="isDetail"
      @confirm="handleSubmit"
    />
    <DialogEnterpriseManagement
      :visible.sync="showEnterpriseDialog"
      :buildId="curBuildId"
      :list="curEnterpriseRelList"
      @confirm="getList"
    />
  </div>
</template>

<script>
import {
  queryCarrierInfo,
  addCarrierInfo,
  deleteCarrierInfo,
  downloadCarrierInfo
} from '@/api/dataManagement'
import Icon from '@/components/icon'
import {
  isScaleEnterpriseOptions,
  isHighNewEnterpriseOptions,
  isSpecialEnterpriseOptions,
  incubationYearOptions
} from '@/utils/project/data'
import AddEnterpriseDialog from './components/addEnterpriseDialog.vue'
import DialogEnterpriseManagement from './components/dialogEnterpriseManagement.vue'
import dayjs from 'dayjs'
import { downloadBlobFile } from '@/utils/download'
import listKeepAliveMixin from '@/mixins/listKeepAliveMixin'

export default {
  name: 'industrialCarrierList',
  mixins: [listKeepAliveMixin],
  components: {
    Icon,
    AddEnterpriseDialog,
    DialogEnterpriseManagement
  },
  data() {
    return {
      projectList: [],
      total: '',
      parameter: {
        name: '',
        type: '产业载体',
        pageIndex: 1,
        pageSize: 10
      },
      projectTypeOptions: [],
      loading: false,
      showDialog: false,
      currentData: {
        pkid: '',
        name: '',
        remark: '产业载体',
        unit: '',
        checkFlag: '是'
      },
      isDetail: false,
      title: '',
      showEnterpriseDialog: false,
      curEnterpriseRelList: [],
      curBuildId: ''
    }
  },
  computed: {
    isScaleEnterpriseOptions() {
      return isScaleEnterpriseOptions
    },
    isHighNewEnterpriseOptions() {
      return isHighNewEnterpriseOptions
    },
    isSpecialEnterpriseOptions() {
      return isSpecialEnterpriseOptions
    },
    incubationYearOptions() {
      return incubationYearOptions
    }
  },
  mounted() {
    this.getList()
  },
  activated() {
    if (this.$route.query.refresh) {
      this.getList()
      this.$router.replace({
        path: this.$route.path,
        query: { ...this.$route.query, refresh: undefined }
      })
    }
  },
  methods: {
    search() {
      this.parameter.name =
        this.parameter.name?.trim?.() || ''
      this.parameter.pageIndex = 1
      this.getList()
    },

    pageChange(page) {
      this.parameter.pageIndex = page
      this.getList()
    },

    // 获取用户列表
    getList() {
      this.parameter.type = '产业载体'
      this.loading = true
      queryCarrierInfo(this.parameter).then((res) => {
        if (res.success) {
          this.total = res.data.count
          this.projectList = res.data.list
        }
        this.loading = false
      })
    },

    // 表格高亮
    tableRowClassName({ rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'light'
      } else {
        return ''
      }
    },

    // 新增
    addProject() {
      this.showDialog = true
      this.isDetail = false
      this.currentData = Object.assign(
        {},
        this.$options.data().currentData
      )
      this.title = '新增'
    },

    // 编辑
    editProject(row) {
      this.showDialog = true
      this.currentData = row
      this.isDetail = false
      this.title = '编辑'
    },
    // 项目查看
    viewProject(row) {
      this.$router.push({
        path: '/dataManagement/carrierReduction/industrialCarrier/detail',
        query: {
          title: '查看',
          pkid: row.pkid,
          name: row.parkName
        }
      })
    },

    trackProject() {
      this.$router.push({
        path: '/dataManagement/carrierReduction/industrialCarrier/process',
        query: {
          title: '进度填报'
        }
      })
    },

    // 删除项目
    deleteProject(row) {
      this.$confirm(
        '此操作将永久删除该项目，是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          deleteCarrierInfo(row.pkid).then((res) => {
            if (res.success) {
              this.$message.success('删除成功')
              if (
                this.total % 10 === 1 &&
                this.parameter.pageIndex ===
                  parseInt(this.total / 10) + 1 &&
                this.parameter.pageIndex !== 1
              ) {
                this.parameter.pageIndex -= 1
              }
              this.getList()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch((error) => {
          this.$message.info('已取消删除')
        })
    },
    downloadFile() {
      this.btnLoading = true
      downloadCarrierInfo(this.parameter)
        .then((res) => {
          downloadBlobFile(res, '产业载体信息表.xlsx')
        })
        .catch((err) => {
          this.$message.error('下载失败')
        })
        .finally(() => {
          this.btnLoading = false
        })
    },
    // 入驻企业编辑
    handleEnterprise(row) {
      // if (!row.enterpriseNum) return
      this.showEnterpriseDialog = true
      this.curBuildId = row.pkid
      this.curEnterpriseRelList =
        row.enterpriseRelList || []
    },
    handleSubmit(data) {
      addCarrierInfo(data).then((res) => {
        if (res.success) {
          this.$message.success(
            data.pkid ? '编辑成功' : '保存成功'
          )
          this.getList()
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/css/table.scss';
.link-enterprise {
  color: #409eff;
  cursor: pointer;
}
</style>
